﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;

namespace DAO
{
    public class TinhTrangXeDAO : DataProvider
    {
        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            TinhTrangXeDTO objectDTO = new TinhTrangXeDTO();
            objectDTO.MaXe = int.Parse(dt.Rows[i]["MaXe"].ToString());
            objectDTO.MaTinhTrang = int.Parse(dt.Rows[i]["MaTinhTrang"].ToString());
            objectDTO.NgayHu = DateTime.Parse(dt.Rows[i]["NgayHu"].ToString());
            objectDTO.ThongTin = dt.Rows[i]["ThongTin"].ToString();
            return (object)objectDTO;
        }

        public TinhTrangXeDTO[] GetAll()
        {
            String strSQL = "Select * from TinhTrang_Xe ";
            DataTable dt = new DataProvider().ExecuteQuery(strSQL);

            TinhTrangXeDTO[] TinhTrangXeDTOs = new TinhTrangXeDTO[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
                TinhTrangXeDTOs[i] = (TinhTrangXeDTO)GetDataFromDataRow(dt, i);

            return TinhTrangXeDTOs;
        }

        public void insert(TinhTrangXeDTO info)
        {
            string insertCommand = "INSERT TinhTrang_Xe VALUES(" +
                info.MaXe + ", '" +
                info.MaTinhTrang + "', " +
                info.NgayHu + "', " +
                info.ThongTin + ")";

            ExecuteNonQuery(insertCommand);
        }

        public void update(TinhTrangXeDTO info)
        {
            string updateCommand = "UPDATE TinhTrang_Xe " +
                                    "SET MaTinhTrang = '" + info.MaTinhTrang + "', " +
                                    " NgayHu = '" + info.NgayHu +
                                    " ThongTin = '" + info.ThongTin +
                                    " WHERE MaXe = " + info.MaXe;


            ExecuteNonQuery(updateCommand);
        }

        public void delete(String MaXe,string maTinhTrang)
        {
            string deleteCommand = "DELETE FROM TinhTrang_Xe WHERE MaXe='"+MaXe+"' AND MaTinhTrang ='" + maTinhTrang+"'";
            ExecuteNonQuery(deleteCommand);
        }
    }
}
